package com.micangcaihang.micang.mvp.task.presenter;

import com.micangcaihang.common.baserx.RxSchedulers;
import com.micangcaihang.common.http.ApiResult;
import com.micangcaihang.common.http.RxHelper;
import com.micangcaihang.common.http.exception.ApiException;
import com.micangcaihang.common.utils.ToastUtils;
import com.micangcaihang.micang.entity.FTaskFaceOpinionLoanConditionEntity;
import com.micangcaihang.micang.http.MiSubScriber;
import com.micangcaihang.micang.mvp.task.contract.FTaskFaceOpinionLoanConditionContract;
import com.micangcaihang.micang.mvp.task.model.FTaskFaceOpinionLoanConditionModel;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/07/17
 *     desc   : 风控-面审意见放款条件
 *     version: 1.0
 * </pre>
 */

public class FTaskFaceOpinionLoanConditionPresenter extends FTaskFaceOpinionLoanConditionContract.Presenter {
    private String id;
    private FTaskFaceOpinionLoanConditionModel mModel;
    private boolean isRefresh;

    @Override
    public void setV(FTaskFaceOpinionLoanConditionContract.View v) {
        super.setV(v);
        mModel = new FTaskFaceOpinionLoanConditionModel();
    }

    public void setId(String id) {
        this.id = id;
    }

    /**
     * 每次进入界面获取网络数据，更新界面
     *
     * @param isRefresh 新增参数，用来判断是否为下拉刷新调用，下拉刷新的时候不应该再显示加载界面和异常界面
     */
    @Override
    public void loadFirstData(boolean isRefresh) {
        this.isRefresh = isRefresh;
        loadFaceOpinionLoanConditionData();
    }

    /**
     * 加载更多数据
     */
    @Override
    public void loadMoreData() {

    }


    /**
     * 加载借款人信息数据
     */
    @Override
    public void loadFaceOpinionLoanConditionData() {
        Observable<ApiResult<FTaskFaceOpinionLoanConditionEntity>> observable = mModel.getFaceOpinionLoanConditionData(id);
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<FTaskFaceOpinionLoanConditionEntity>>() {

            @Override
            protected void doOnStart() {
                mView.showLoading();
            }

            @Override
            protected void doOnNext(ApiResult<FTaskFaceOpinionLoanConditionEntity> result) {
                if (result.data != null) {
                    mView.returnFaceOpinionLoanConditionData(result.data);
                }
            }

            @Override
            protected void doOnComplete() {
                mView.showContent();
            }

            @Override
            protected void doOnError(ApiException e) {
//                super.doOnError(e);
                mView.showNetError(e);
            }
        }));
    }

    /**
     * 保存提交保存信息
     *
     * @param request
     */
    @Override
    public void saveFaceOpinionLoanCondition(FTaskFaceOpinionLoanConditionEntity request) {
        Observable<ApiResult<FTaskFaceOpinionLoanConditionEntity>> opinionLoanConditionData = mModel.getFaceOpinionLoanConditionData(id);
        Observable<ApiResult<Void>> observable = mModel.savaFaceOpinionLoanConditionData(request);
        mRxManage.add(observable.concatMap(new Function<ApiResult<Void>, ObservableSource<ApiResult<FTaskFaceOpinionLoanConditionEntity>>>() {
            @Override
            public ObservableSource<ApiResult<FTaskFaceOpinionLoanConditionEntity>> apply(@NonNull ApiResult<Void> voidApiResult) throws Exception {
                return opinionLoanConditionData;
            }
        }).compose(RxSchedulers.observableIoToMain()).compose(RxHelper.handleResult())
                .subscribeWith(new MiSubScriber<ApiResult<FTaskFaceOpinionLoanConditionEntity>>() {

                    @Override
                    protected void doOnStart() {
                        mView.showDialogLoading("");
                    }

                    @Override
                    protected void doOnNext(ApiResult<FTaskFaceOpinionLoanConditionEntity> result) {
                        if (result.data != null) {
                            mView.returnFaceOpinionLoanConditionData(result.data);
                            ToastUtils.showLong("保存成功");
                        }
                    }

                    @Override
                    protected void doOnComplete() {
                        mView.hideDialogLoading();
                    }

                    @Override
                    protected void doOnError(ApiException e) {
                        super.doOnError(e);
                        mView.hideDialogLoading();
                    }
                }));
    }
}
